﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ThoiTrang.Business;
using ThoiTrang.Entities;

/// <summary>
/// Summary description for SessionManager
/// </summary>
public class SessionManager
{
    public SessionManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    private static string cookieID;
    public static bool IsLogged;
    public static int UserTypeId;
    public static string GetCookie()
    {
        //return HttpContext.Current.Session["TTCookie"] as String;
        return cookieID;
    }

    public static void SetCookie(string cookieVal)
    {
        cookieID = cookieVal;
    }
    public static void CheckLogin(string ckid, string ip, string filename)
    {
      a_Session ss = null;
      if (String.IsNullOrEmpty(ckid))
      {
        SessionManager.SetCookie(TTHelper.GetRandomCkid(filename, ip));
        ss = a_SessionService.Geta_Session(SessionManager.GetCookie());
      }
      else
      {
        ss = a_SessionService.Geta_Session(ckid);
        if (ss == null || (DateTime.Now - ss.Time_stamp).TotalMinutes > FOConstants.SessionTimeout || ss.RemoteIP != ip)
        {
          SessionManager.SetCookie(TTHelper.GetRandomCkid(filename, ip));
          ss = a_SessionService.Geta_Session(SessionManager.GetCookie());
        }
        else
        {
          SessionManager.SetCookie(ckid);
        }
      }
      if (ss.Login_Status == false || (DateTime.Now - ss.Time_stamp).TotalMinutes > FOConstants.SessionTimeout)
      {
        SessionManager.IsLogged = false;
        ss.Login_Status = false;
      }
      else
      {
        SessionManager.IsLogged = true;
        ss.Login_Status = true;
        //SessionManager.Level 
        a_User usr = a_UserService.Geta_UserBySSID(SessionManager.GetCookie());
        UserTypeId = usr.UserTypeID;
      }
      ss.Time_stamp = DateTime.Now;
      a_SessionService.Updatea_Session(ss);
    }
}
